home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-4 / screen2.c < prev    next >
C/C++ Source or Header  |  1997-08-31  |  9KB  |  301 lines

  1. /* First, include our own supporting header files, i.e., clip.h */
  2. #include "clip.h"
  3.  
  4. #include<exec/libraries.h>
  5. #include<intuition/intuition.h>
  6. #include<utility/tagitem.h>
  7. #include<graphics/text.h>
  8. #include<graphics/rastport.h>
  9. #include<intuition/screens.h>
  10. #include<libraries/gadtools.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/exec_protos.h>
  16. #include<clib/graphics_protos.h>
  17. #include<clib/intuition_protos.h>
  18. #include<clib/gadtools_protos.h>
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* LayersBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26.  
  27. /* The global handle on the palette gadget */
  28. struct Gadget* palgad = NULL;
  29.  
  30. /* Need to give prototypes for our functions */
  31. void handleIDCMP(struct Window*);
  32. void setupWindow();
  33. void createWindow(struct Gadget*, struct Screen*);
  34. int openLibs();
  35. void closeLibs();
  36.  
  37. /* Some constants for the position and size of our gadget */
  38. #define MYBUT_LEFT        (10)
  39. #define MYBUT_TOP            (5)
  40. #define MYBUT_WIDTH        (80)
  41. #define MYBUT_HEIGHT    (12)
  42. #define MYBUT_TEXT        "Next Pen"
  43. #define MYBUT_ID            (0)
  44.  
  45. #define MYPAL_LEFT        (170)
  46. #define MYPAL_TOP            (2)
  47. #define MYPAL_WIDTH        (109)
  48. #define MYPAL_HEIGHT    (19)
  49. #define MYPAL_TEXT        "Colour:"
  50. #define MYPAL_ID            (1)
  51. #define MYPAL_DEPTH        (4)
  52.  
  53. /* The top gap required around the gadgets */
  54. #define MYTOPGAP      (30)
  55.  
  56. /* The initial pen colour */
  57. #define MYINITPEN            (1)
  58.  
  59. /* The start of the program */
  60. void main()
  61. {
  62.     /* Use a different style of opening libraries... */
  63.     if(openLibs())
  64.     {
  65.         /* Now do the real work */
  66.         setupWindow();
  67.     }
  68.     /* Matched call to close libraries */
  69.     closeLibs();
  70. }
  71.  
  72. /* Try to open all the libraries -- return TRUE on success */
  73. int openLibs()
  74. {
  75.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  76.     {
  77.         printf("Error: could not open graphics.library\n");
  78.         return FALSE;
  79.     }
  80.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  81.     {
  82.         printf("Error: could not open intuition.library\n");
  83.         return FALSE;
  84.     }
  85.     if((LayersBase = OpenLibrary("layers.library",37)) == NULL)
  86.     {
  87.         printf("Error: could not open layers.library\n");
  88.         return FALSE;
  89.     }
  90.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  91.     {
  92.         printf("Error: could not open gadtools.library\n");
  93.         return FALSE;
  94.     }
  95.   return TRUE;
  96. }
  97.  
  98. /* Close any open library */
  99. void closeLibs()
  100. {
  101.     if(GadToolsBase)
  102.         CloseLibrary(GadToolsBase);
  103.     if(LayersBase)
  104.         CloseLibrary(LayersBase);
  105.     if(IntuitionBase)
  106.         CloseLibrary(IntuitionBase);
  107.     if(GfxBase)
  108.         CloseLibrary(GfxBase);
  109. }
  110.  
  111. /* Setup the window -- do the GadTools stuff */
  112. void setupWindow()
  113. {
  114.     struct Screen* scr;
  115.     UWORD pens[] = { ~0 };
  116.     /* Try to open a new screen with 16 colours (four bitplanes deep) */
  117.     if(scr = OpenScreenTags(NULL,
  118.                                                     SA_Depth,    4,
  119.                                                     /* Enable 3D look by specifying SA_Pens */
  120.                                                     SA_Pens,    pens,
  121.                                                     TAG_DONE))
  122.     {
  123.         APTR vinfo;
  124.         /* Get the visual info so GadTools can render the gadgets nicely */
  125.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  126.         {
  127.             /* We can initialise glist in its declaration */
  128.             struct Gadget* glist = NULL;
  129.             struct Gadget* gad;
  130.             int offtop, offleft;
  131.             struct NewGadget newgad;
  132.             /* Initialised structure declaration: describes 8pt Topaz font */
  133.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  134.             /* Start a GadTools gadget list */
  135.             gad = CreateContext(&glist);
  136.             /* The offsets of our window borders */
  137.             offleft = scr->WBorLeft;
  138.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  139.  
  140.             /* Setup our first gadget */
  141.             newgad.ng_TextAttr         = &topazFont;
  142.             newgad.ng_VisualInfo     = vinfo;
  143.             newgad.ng_LeftEdge         = MYBUT_LEFT + offleft;
  144.             newgad.ng_TopEdge         = MYBUT_TOP + offtop;
  145.             newgad.ng_Width             = MYBUT_WIDTH;
  146.             newgad.ng_Height             = MYBUT_HEIGHT;
  147.             newgad.ng_GadgetText    = MYBUT_TEXT;
  148.             newgad.ng_GadgetID        = MYBUT_ID;
  149.             newgad.ng_Flags                = 0;
  150.             /* Now create it and add it to our list */
  151.             gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
  152.  
  153.             /* Setup our second gadget */
  154.             /* (We can reuse newgad, and just change the different bits) */
  155.             newgad.ng_LeftEdge         = MYPAL_LEFT + offleft;
  156.             newgad.ng_TopEdge         = MYPAL_TOP + offtop;
  157.             newgad.ng_Width             = MYPAL_WIDTH;
  158.             newgad.ng_Height             = MYPAL_HEIGHT;
  159.             newgad.ng_GadgetText    = MYPAL_TEXT;
  160.             newgad.ng_GadgetID        = MYPAL_ID;
  161.             newgad.ng_Flags                = 0;
  162.             /* Now create it and add it to our list */
  163.             if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
  164.                                                         /* Initially selected pen */
  165.                                                         GTPA_Color, MYINITPEN,
  166.                                                         /* Depth: 2 to the power MYPAL_DEPTH colours */
  167.                                                         GTPA_Depth, MYPAL_DEPTH,
  168.                                                         /* Gadget will indicate selection */
  169.                                                         GTPA_IndicatorWidth, 16,
  170.                                                         TAG_DONE))
  171.             {
  172.                 /* Remember gadget pointer so we can affect it in message handler */
  173.                 palgad = gad;
  174.                 /* If succeeded then all gadgets created */
  175.                 createWindow(glist, scr);
  176.             }
  177.             else
  178.                 printf("Error: could not create gadget(s)\n");
  179.             /* Free all the gadgets that were created */
  180.             FreeGadgets(glist);
  181.             FreeVisualInfo(vinfo);
  182.         }
  183.         else
  184.             printf("Error: could not get visual info\n");
  185.         CloseScreen(scr);
  186.     }
  187.     else
  188.         printf("Error: could not create screen\n");
  189. }
  190.  
  191. /* Actually open the window, in the normal way */
  192. void createWindow(struct Gadget* glist, struct Screen* scr)
  193. {
  194.     struct Window* win;
  195.     /* Open our window */
  196.     if(win = OpenWindowTags(NULL,
  197.                                                     WA_Left,                    0,
  198.                                                     WA_Top,                        0,
  199.                                                     /* Make the window the same size as the screen */
  200.                                                     WA_Width,                    scr->Width,
  201.                                                     WA_Height,                scr->Height,
  202.                                                     WA_Flags,                    WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  203.                                                     WA_IDCMP,                    IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW,
  204.                                                     WA_Gadgets,                glist,
  205.                                                     WA_CustomScreen,    scr,
  206.                                                     TAG_DONE,                    0))
  207.     {
  208.         /* If window opened, set clip region */
  209.         if(setClipInternal(win))
  210.         {
  211.             /* Let GadTools refresh its bits of the window */
  212.             GT_RefreshWindow(win, NULL);
  213.             /* Now handle messages */
  214.             handleIDCMP(win);
  215.             removeClip(win);
  216.         }
  217.         else
  218.             printf("Error: could not set clip region on window\n");
  219.         CloseWindow(win);
  220.     }
  221.     else
  222.         printf("Error: could not open window\n");
  223. }
  224.  
  225. /* Our message handling code */
  226. void handleIDCMP(struct Window* win)
  227. {
  228.     char* text = "Hello World!";
  229.     int going = TRUE;
  230.     int drawing = FALSE;
  231.     UBYTE pen = MYINITPEN;
  232.     SetAPen(win->RPort, pen);
  233.     /* Set the drawing mode to draw only the foreground of text, not the background */
  234.     SetDrMd(win->RPort, JAM1);
  235.     while(going)
  236.     {
  237.         struct IntuiMessage* intuimsg;
  238.         /* Wait for messages to arrive */
  239.         WaitPort(win->UserPort);
  240.         /* Messages have arrived: loop through all of them */
  241.         while(intuimsg = GT_GetIMsg(win->UserPort))
  242.         {
  243.             /* Act on this message... */
  244.             switch(intuimsg->Class)
  245.             {
  246.             case IDCMP_MOUSEBUTTONS:
  247.                 switch(intuimsg->Code)
  248.                 {
  249.                 case SELECTDOWN:
  250.                     drawing = TRUE;
  251.                     break;
  252.                 case SELECTUP:
  253.                     drawing = FALSE;
  254.                     break;
  255.                 }
  256.                 /* break; omitted so we draw on click, too */
  257.             case IDCMP_MOUSEMOVE:
  258.                 /* Don't draw on top gap which holds gadgets */
  259.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  260.                 {
  261.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  262.                     Text(win->RPort, text, strlen(text));
  263.                 }
  264.                 break;
  265.             case IDCMP_CLOSEWINDOW:
  266.                 going = FALSE;
  267.                 break;
  268.             case IDCMP_REFRESHWINDOW:
  269.                 /* You *MUST* remember to ask for and handle these refresh messages */
  270.                 GT_BeginRefresh(win);
  271.                 GT_EndRefresh(win, TRUE);
  272.                 break;
  273.             case IDCMP_GADGETUP:
  274.                 /* Trick: introduce new "{..}" scope so we can declare gad locally */
  275.                 {
  276.                     struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
  277.                     switch(gad->GadgetID)
  278.                     {
  279.                     case MYBUT_ID:
  280.                         /* Our button was clicked!  Set foreground to next pen colour */
  281.                         /* (Wrap when reached the end of the palette gadget's colours) */
  282.                         pen = (pen+1) % (1<<MYPAL_DEPTH);
  283.                         SetAPen(win->RPort, pen);
  284.                         /* Update palette gadget with new pen value */
  285.                         GT_SetGadgetAttrs(palgad, win, NULL, GTPA_Color, pen, TAG_DONE);
  286.                         break;
  287.                     case MYPAL_ID:
  288.                         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  289.                         pen = intuimsg->Code;
  290.                         SetAPen(win->RPort, pen);
  291.                         break;
  292.                     }
  293.                     break;
  294.                 }
  295.             }
  296.             /* Reply when finished with message */
  297.             GT_ReplyIMsg(intuimsg);
  298.         }
  299.     }
  300. }
  301.